/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.resource {

	/**
	 * <code>ResourceProgress</code> provides detailled information on the loading 
	 * progress of the resources.
	 * In contrast to the also distributed <code>QEvent.PROGRESS</code> the
	 * <code>ResourceProgress</code> also contains the exact overall progress while
	 * <code>QEvent.PROGRESS</code> only estimates the overall progress based on
	 * the number of items within the <code>IQ</code> 
	 * 
	 * @author Max Kugland
	 */
	public class ResourceProgress {
		private var _currentId : String;
		private var _current : int;
		private var _total : int;
		private var _currentLoadedBytes : int;
		private var _currentTotalBytes : int;
		private var _totalLoadedBytes : int;
		private var _totalBytes : int;

		/**
		 * @param id the id of the resource which is currently processed
		 * @param current this resource's sequence number
		 * @param currentItemTotalBytes this resource's total bytes
		 * @param totalLoadedBytes the amount of bytes which have been loaded before this resource
		 * is loaded. (this is helpful to calculate the overall progress)
		 */
		public function ResourceProgress(currentId : String, current : int, currentTotalBytes : int, totalLoadedBytes : int) {
			_currentId = currentId;
			_current = current;
			_currentTotalBytes = currentTotalBytes;
			_totalLoadedBytes = totalLoadedBytes;
		}

		/**
		 * @return the total number of resources
		 */
		public function get total() : int {
			return _total;
		}

		/**
		 * @return the resource which is currently processed
		 */
		public function get current() : int {
			return _current;
		}

		/**
		 * @return the id of of the resource which is currently processed
		 */
		public function get currentId() : String {
			return _currentId;
		}

		/**
		 * @return the amount of bytes which has been loaded from the current resource
		 */
		public function get currentLoadedBytes() : int {
			return _currentLoadedBytes;
		}

		internal function setCurrentLoadedBytes(bytes : int) : void {
			_currentLoadedBytes = bytes;
		}

		/**
		 * @return the amount of percent which has been loaded from the current resource
		 */
		public function get currentPercent() : int {
			return Math.max(0, Math.min(1, (_currentLoadedBytes / _currentTotalBytes))) * 100;
		}

		internal function setTotalItems(totalItems : int) : void {
			_total = totalItems;
		}

		/**
		 * @return the total amount of bytes of all resources
		 */
		public function get totalBytes() : int {
			return _totalBytes;
		}

		internal function setTotalBytes(totalBytes : int) : void {
			_totalBytes = totalBytes;
		}

		/**
		 * @return the amount of percent which has been loaded of all resources
		 */
		public function get totalPercent() : int {
			return Math.max(0, Math.min(1, ((_totalLoadedBytes + _currentLoadedBytes) / _totalBytes))) * 100;
		}

		/**
		 * @return the total amount of bytes of the current resource 
		 */
		public function get currentTotalBytes() : int {
			return _currentTotalBytes;
		}
		
		/**
		 * @return the current amount of bytes which has been loaded of all resources 
		 */
		public function get totalLoadedBytes() : int {
			return _totalLoadedBytes + _currentLoadedBytes;
		}

		/**
		 * @return a <code>String</code> representation of the instance
		 */
		public function toString() : String {
			return "current: " + currentLoadedBytes + " / " + currentTotalBytes + " : " + currentPercent + "% | total: " + totalLoadedBytes + " / " + totalBytes + " : " + totalPercent + "%";
		}
	}
}
